package com.example.hms.awareness;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.widget.SwitchCompat;

import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.kit.awareness.Awareness;
import com.huawei.hms.kit.awareness.barrier.AmbientLightBarrier;
import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

public class BarrierUtil {
    private static final String TAG = BarrierUtil.class.getSimpleName();
    private static String lightBarrierLabel = "light above barrier";

    public static void createBarrier(Activity activity, SensorUtil sensorUtil,
                                     SwitchCompat openOrClose) {
        final float luxValue = 600.0f;
        AwarenessBarrier lightAboveBarrier = AmbientLightBarrier.above(luxValue);

        final String BARRIER_RECEIVER_ACTION = activity.getPackageName() + ".LIGHT_BARRIER_RECEIVER_ACTION";
        Log.d(TAG, BARRIER_RECEIVER_ACTION);
        Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
        PendingIntent pendingIntent =
            PendingIntent.getBroadcast(activity, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        LightBarrierReceiver barrierReceiver = new LightBarrierReceiver();
        activity.registerReceiver(barrierReceiver, new IntentFilter(BARRIER_RECEIVER_ACTION));

        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request = builder.addBarrier(lightBarrierLabel, lightAboveBarrier, pendingIntent).build();
        Awareness.getBarrierClient(activity)
            .updateBarriers(request)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "add barrier success");
                    sensorUtil.start();
                    Toast.makeText(activity, "开启成功", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "add barrier failed", e);
                    Toast.makeText(activity, "开启失败", Toast.LENGTH_SHORT).show();
                    openOrClose.setChecked(false);
                }
            });
    }

    public static void deleteBarrier(Activity activity, SensorUtil sensorUtil) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request = builder.deleteBarrier(lightBarrierLabel).build();
        Awareness.getBarrierClient(activity)
            .updateBarriers(request)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "delete barrier success");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "delete barrier failed", e);
                }
            })
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(Task<Void> task) {
                    sensorUtil.stop();
                }
            });
    }
}
